<?php
/*
Extension Name: Vcal
Extension Url: N/A
Description: Integrated event calendar
Version: 0.1
Author: Ziyad (MySchizoBuddy) Saeed
Author Url: N/A
*/
/*
 Changelog

*/

// Vcal root path;
if (!defined('VCAL_ROOT')) define('VCAL_ROOT', dirname(__FILE__).'/');
if (!defined('CALENDAR_ENGINE')) define('CALENDAR_ENGINE', 'PearDate');
if (!defined('CALENDAR_ROOT')) define('CALENDAR_ROOT', VCAL_ROOT.'/library/calendar/');
if (!defined('THEME_ROOT')) define('THEME_ROOT', VCAL_ROOT.'/theme/');


//Define Global variables
global $Context, $Configuration, $Menu;

// Vcal Preferences
if( !array_key_exists('VCAL_VERSION', $Configuration)) {AddConfigurationSetting($Context, 'VCAL_VERSION', '0.1');}
if( !array_key_exists('VCAL_DEFAULT_VIEW', $Configuration)) {AddConfigurationSetting($Context, 'VCAL_DEFAULT_VIEW', 'month');}
if( !array_key_exists('VCAL_Calendar_ID', $Configuration)) {AddConfigurationSetting($Context, 'VCAL_Calendar_ID', '1');}
if( !array_key_exists('VCAL_DEFAULT_THEME', $Configuration)) {AddConfigurationSetting($Context, 'VCAL_DEFAULT_THEME', 'Vcal');}

//Required library path
require_once VCAL_ROOT . 'language/English.php';
require_once VCAL_ROOT . 'library/Class.Calendars.php';
require_once VCAL_ROOT . 'library/Class.CalendarManager.php';
require_once VCAL_ROOT . 'library/Class.Event.php';
require_once VCAL_ROOT . 'library/Class.EventManager.php';

//Role Permissions
$Context->Configuration['PERMISSION_VCAL_VIEW_CALENDAR_TAB'] = '0';
$Context->Configuration['PERMISSION_VCAL_ADD_EVENT'] = '0';
$Context->Configuration['PERMISSION_VCAL_DELETE_EVENT'] = '0';
$Context->Configuration['PERMISSION_VCAL_ADD_CALENDAR'] = '0';
$Context->Configuration['PERMISSION_VCAL_EDIT_CALENDAR'] = '0';
$Context->Configuration['PERMISSION_VCAL_DELETE_CALENDAR'] = '0';
$Context->Configuration['PERMISSION_VCAL_SORT_CALENDAR'] = '0';

/**
 * Add an Event Calendar tab to users who have the required permission
 */
if (isset($Menu) && $Context->Session->User->Permission('PERMISSION_VCAL_VIEW'))
    $Menu->AddTab($Context->GetDefinition('VcalTabName'), 'Vcal', GetUrl($Configuration, 'extension.php', '', 'PostBackAction', 'Vcal', '',  ''), '', '');

/**
 * Render the Calendar grid to users who have the required permission
 */
if ($Context->SelfUrl == 'extension.php' && ForceIncomingString('PostBackAction', '') == 'Vcal' && $Context->Session->User->Permission('PERMISSION_VCAL_VIEW_CALENDAR_TAB')) {
    require_once VCAL_ROOT . 'library/Class.Event.php';
    require_once VCAL_ROOT . 'library/Class.EventManager.php';
    require_once VCAL_ROOT . 'library/Control.MonthView.php';
    //require_once VCAL_ROOT . 'library/Control.DailyView.php';
    require_once VCAL_ROOT . 'library/Panel.CalendarFilter.php';

    $Context->Dictionary['StartANewDiscussion'] = 'Add a new Event';
    // Add Calendar specific stylesheet to <Head>
    $Head->AddStyleSheet('extensions/Vcal/theme/calendarstyle.css');
    // When you visit the extension.php page the Vcal tab should be highlighted
    $Menu->CurrentTab = 'Vcal';
    // Add a PageTitle. The title that appears on the Window bar
    $Context->PageTitle =  $Context->GetDefinition('VcalPageTitle');
    //Disable Whispers. Events cannot be whispered
    $Context->Configuration['ENABLE_WHISPERS'] = '0';
    // Initialize the Month View
    $MonthView = $Context->ObjectFactory->NewContextObject($Context, 'MonthView');
    // Add the Month view to the page at Position_Body_item
    $Page->AddRenderControl($MonthView, $Configuration['CONTROL_POSITION_BODY_ITEM']);
    // Initialize the Day View
 //   $DailyView = $Context->ObjectFactory->NewContextObject($Context, 'DailyView');
    // Add the Day view to the page at Position_Body_item
  //  $Page->AddRenderControl($DailyView, $Configuration['CONTROL_POSITION_BODY_ITEM']+10);
    
}

/**
 * Add new Event Form specific elements to Discussion Form
 */
if ($Context->SelfUrl == 'post.php' && $Context->Session->User->Permission('PERMISSION_VCAL_ADD_EVENT') && $Context->Session->UserID && ForceIncomingInt('CategoryID', '') == $Context->Configuration['VCAL_Calendar_ID']){ //check for user permission

    $Context->Dictionary['EnterYourDiscussionTopic'] = 'Enter the event name';
    $Context->Dictionary['EditYourDiscussionTopic'] = 'Edit the event name';
    $Context->Dictionary['StartANewDiscussion'] = 'Add a new Event';
    $Context->Dictionary['StartYourDiscussion'] = 'Submit your Event';
    // When you visit the post.php page the Vcal tab should be highlighted
    $Menu->CurrentTab = 'Vcal';
    // Add Event Form specific stylesheet to <Head>
    $Head->AddStyleSheet('extensions/Vcal/theme/formstyle.css');
    // Add Date picker specific stylesheet to <Head>
    $Head->AddStyleSheet('extensions/Vcal/theme/ui.datepicker.css');
    // Support for low-cal Vanilla, which compresses javascript for fast loading. Low-cal vanilla etension has to be enabled before this extension
    //if ($Configuration['LOWCALVANILLA_TOOLS_PATH']) {
	//require_once($Configuration['LOWCALVANILLA_TOOLS_PATH']);
    //    LowCalVanilla_AddScript($Context, $Head, 'extensions/Vcal/js/jquery-1.2.1.pack.js', 100);
    //	LowCalVanilla_AddScript($Context, $Head, 'extensions/Vcal/js/ui.datepicker.js');
    //} else {
        $Head->AddScript('extensions/Vcal/js/jquery-1.2.1.pack.js');
        $Head->AddScript('extensions/Vcal/js/ui.datepicker.js');
	$Head->AddScript('extensions/Vcal/js/EventForm.js.php');
    //}
    
    /**
     * Initialize Event Manager
     */
    function DiscussionForm_InitializeEventManager(&$DiscussionForm) {
	$EventManager = $DiscussionForm->Context->ObjectFactory->NewContextObject($DiscussionForm->Context, 'EventManager');
	$DiscussionForm->DelegateParameters['EventManager'] = &$EventManager;
    }
    // Attach EventManager instance to the DiscussionForm delegate
    $Context->AddToDelegate("DiscussionForm","PreLoadData","DiscussionForm_InitializeEventManager");

    /**
     * Render Event Form
     */    
    function DiscussionForm_RenderEventForm(&$DiscussionForm) {
	$EventManager = &$DiscussionForm->DelegateParameters['EventManager'];
	$EventManager->FormName = 'frmPostDiscussion';
	$EventManager->CommentID = &$DiscussionForm->Comment->CommentID;
        $EventManager->DiscussionID = &$DiscussionForm->Comment->DiscussionID;
	$EventManager->RenderEventForm();
    }
    // Attach Event Form to the DiscussionForm  delegate 
    $Context->AddToDelegate("DiscussionForm","DiscussionForm_PreCommentRender","DiscussionForm_RenderEventForm");

    /**
     * Save Event to database
     */      
    function DiscussionForm_SaveEventToDatabase(&$DiscussionForm) {
	$Discussion = &$DiscussionForm->DelegateParameters['ResultDiscussion'];
	$EventManager = &$DiscussionForm->DelegateParameters['EventManager'];
	$EventManager->DiscussionID = &$Discussion->DiscussionID;
	$EventManager->Discussion = &$Discussion;
	$EventManager->SaveEventToDatabase();
    }
    // Attach Save Event to database to the DiscussionForm delegate    
    $Context->AddToDelegate('DiscussionForm', 'PostSaveDiscussion', 'DiscussionForm_SaveEventToDatabase');

}
if ($Context->SelfUrl == "settings.php" &&
    ($Context->Session->User->Permission('PERMISSION_VCAL_ADD_CALENDAR')
     || $Context->Session->User->Permission('PERMISSION_VCAL_EDIT_CALENDAR')
     || $Context->Session->User->Permission('PERMISSION_VCAL_DELETE_CALENDAR'))) {
    
    $VcalOptions = $Context->GetDefinition('VcalOptions');
    $Panel->AddList($VcalOptions, 20);
    $Panel->AddListItem($VcalOptions, $Context->GetDefinition('VcalCalendars'), GetUrl($Context->Configuration, 'settings.php', '', 'PostBackAction', 'Calendars'));
    $Panel->AddListItem($VcalOptions, $Context->GetDefinition('VcalCalendarSettings'), GetUrl($Context->Configuration, 'settings.php', '', 'PostBackAction', 'CalendarSettings'));
    $Panel->AddListItem($VcalOptions, $Context->GetDefinition('VcalCalendarThemes'), GetUrl($Context->Configuration, 'settings.php', '', 'PostBackAction', 'Themes'));

    if (in_array(ForceIncomingString('PostBackAction', ''), array('Calendars', 'Calendar', 'ProcessCalendar', 'CalendarRemove', 'ProcessCalendarRemove'))) {
        require_once VCAL_ROOT . 'library/Control.CalendarForm.php';
        $CalendarForm = $Context->ObjectFactory->NewContextObject($Context, 'CalendarForm');
        $Page->AddRenderControl($CalendarForm, $Configuration["CONTROL_POSITION_BODY_ITEM"]);
    }
    if (in_array(ForceIncomingString('PostBackAction', ''), array('Calendar', 'ProcessCalendar'))) {
        $Head->AddScript('extensions/Vcal/js/jquery-1.2.1.pack.js');
        $Head->AddScript('extensions/Vcal/js/farbtastic.js');
        $Head->AddScript('extensions/Vcal/js/color.js');
        $Head->AddScript('extensions/Vcal/js/EditCalendar.js');
        $Head->AddStyleSheet('extensions/Vcal/theme/farbtastic.css');
        $Head->AddStyleSheet('extensions/Vcal/theme/formstyle.css');

    }
    
    if (in_array(ForceIncomingString('PostBackAction', ''), array('Themes','ProcessThemes'))) {
        require_once VCAL_ROOT . 'library/Control.ThemesForm.php';
        $Head->AddStyleSheet('extensions/Vcal/theme/formstyle.css');
        $ThemesForm = $Context->ObjectFactory->NewContextObject($Context, 'ThemesForm');
        $Page->AddRenderControl($ThemesForm, $Configuration["CONTROL_POSITION_BODY_ITEM"]);
    }
}
?>
